﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcControlToolkit
{
    /// <summary>
    /// PopupControl is an AJAX extender that can be attached to any control in order 
    /// to open a popup window that displays additional content. The popup window can
    /// contain any content, including HTML elements, etc. 
    /// </summary>
    public static class PopupControlExtensions
    {
        private static readonly PopupControlPopupPosition DEFAULT_POSITION = PopupControlPopupPosition.Center;

        /// <summary>
        /// Creates a PopupControl extender 
        /// </summary>
        /// <param name="elementId">The ID of the control to attach to</param>
        /// <param name="popupControlId">The ID of the control to display</param>
        public static string PopupControl(this AjaxHelper helper, string elementId, string popupControlId)
        {
            return helper.PopupControl(elementId, popupControlId, DEFAULT_POSITION);
        }

        /// <summary>
        /// Creates a PopupControl extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the control to attach to</param>
        /// <param name="popupControlId">The ID of the control to display</param>        
        /// <param name="position">Optional setting specifying where the popup should be positioned relative to the target control. (Left, Right, Top, Bottom, Center (default))</param>        
        public static string PopupControl(this AjaxHelper helper, string elementId, string popupControlId, PopupControlPopupPosition position)
        {
            return helper.PopupControl(elementId, popupControlId, 0, 0, position);
        }

        /// <summary>
        /// Creates a PopupControl extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the control to attach to</param>
        /// <param name="popupControlId">The ID of the control to display</param>
        /// <param name="offsetX">The number of horizontal pixels to offset the Popup from its default position, as specified by Position</param>
        /// <param name="offsetY">The number of vertical pixels to offset the Popup from its default position, as specified by Position</param>
        /// <param name="position">Optional setting specifying where the popup should be positioned relative to the target control. (Left, Right, Top, Bottom, Center (default))</param>        
        public static string PopupControl(this AjaxHelper helper, string elementId, string popupControlId, int offsetX, int offsetY, PopupControlPopupPosition position)
        {
            return helper.PopupControl(elementId, popupControlId, offsetX, offsetY, position, "");
        }

        /// <summary>
        /// Creates a PopupControl extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the control to attach to</param>
        /// <param name="popupControlId">The ID of the control to display</param>
        /// <param name="offsetX">The number of horizontal pixels to offset the Popup from its default position, as specified by Position</param>
        /// <param name="offsetY">The number of vertical pixels to offset the Popup from its default position, as specified by Position</param>
        /// <param name="position">Optional setting specifying where the popup should be positioned relative to the target control. (Left, Right, Top, Bottom, Center (default))</param>
        /// <param name="commitScript">Optional setting specifying additional script to run after setting the result of the popup</param>
        public static string PopupControl(this AjaxHelper helper, string elementId, string popupControlId, int offsetX, int offsetY, PopupControlPopupPosition position, string commitScript)
        {
            return helper.PopupControl(elementId, popupControlId, offsetX, offsetY, position, commitScript, "");
        }

        /// <summary>
        /// Creates a PopupControl extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the control to attach to</param>
        /// <param name="popupControlId">The ID of the control to display</param>
        /// <param name="offsetX">The number of horizontal pixels to offset the Popup from its default position, as specified by Position</param>
        /// <param name="offsetY">The number of vertical pixels to offset the Popup from its default position, as specified by Position</param>
        /// <param name="position">Optional setting specifying where the popup should be positioned relative to the target control. (Left, Right, Top, Bottom, Center (default))</param>
        /// <param name="commitScript">Optional setting specifying additional script to run after setting the result of the popup</param>
        /// <param name="commitProperty">Optional setting specifying the property on the control being extended that should be set with the result of the popup</param>
        public static string PopupControl(this AjaxHelper helper, string elementId, string popupControlId, int offsetX, int offsetY, PopupControlPopupPosition position, string commitScript, string commitProperty)
        {
            RouteValueDictionary props = new RouteValueDictionary();
            props.Add("PopupControlID", popupControlId);
            props.AddIfNotEqual("Position", position, DEFAULT_POSITION);
            props.AddIfNotDefault("OffsetX", offsetX);
            props.AddIfNotDefault("OffsetY", offsetY);
            props.AddIfNotDefault("CommitScript", commitScript);
            props.AddIfNotDefault("CommitProperty", commitProperty);

            return helper.PopupControl(elementId, props);
        }

        /// <summary>
        /// Creates a PopupControl extender with the specified properties
        /// </summary>
        /// <param name="elementId">The ID of the control to attach to</param>
        /// <param name="properties">An object containing the properties to be set</param>
        public static string PopupControl(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();
                        
            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.Animation.Animations.js",
                    "AjaxControlToolkit.PopupExtender.PopupBehavior.js",
                    "AjaxControlToolkit.Animation.AnimationBehavior.js",                    
                    "AjaxControlToolkit.Compat.Timer.Timer.js",
                    "AjaxControlToolkit.PopupExtender.PopupBehavior.js",
                    "AjaxControlToolkit.PopupControl.PopupControlBehavior.js"
                ));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.PopupControlBehavior", properties, elementId));

            return sb.ToString();
        }
    }

    #region [Enumerations]

    public enum PopupControlPopupPosition
    {
        Center = 0,
        Top = 1,
        Left = 2,
        Bottom = 3,
        Right = 4
    }

    #endregion
}
